package com.itbbfx.service.warehourse.impl;

import com.itbbfx.entity.*;
import com.itbbfx.mapper.DrugstoreMapper;
import com.itbbfx.mapper.RecordMapper;
import com.itbbfx.service.warehourse.DrugstoreService;
import com.itbbfx.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class DrugstoreServiceImpl implements DrugstoreService {
    @Autowired
    private DrugstoreMapper drugstoreMapper;
    @Autowired
    private RecordMapper recordMapper;

    @Override
    public int addDrugstoreNum(Drugstore drugstore) {
        Map<String, Object> map = new HashMap<>();
        map.put("drugstoreName", drugstore.getDrugstoreName());
        map.put("warehouseId", drugstore.getWarehouseId());
        map.put("sellingPrice", drugstore.getSellingPrice());
        map.put("produceDate", drugstore.getProduceDate());
        map.put("validDate", drugstore.getValidDate());

        DrugstoreVo drugstoreVo = drugstoreMapper.queryvalidDate(map);
        if (drugstoreVo != null) {
            if (0 < drugstoreVo.getRugstoreId()) {
                drugstoreVo.setBatch(drugstore.getBatch());
                drugstoreVo.setDrugstorenum(drugstoreVo.getDrugstorenum() + drugstore.getDrugstorenum());
                return drugstoreMapper.updateDrugstoreNum(drugstoreVo);
            }
        }

        return drugstoreMapper.addDrugstoreNum(drugstore);
    }

    @Override
    public int queryOverdueCount() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 3);
        return drugstoreMapper.queryOverdueCount(calendar.getTime());
    }

    @Override
    public List<Drugstore> queryOverdue() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 3);
        return drugstoreMapper.queryOverdue(calendar.getTime());
    }

    @Override
    public int delDrugstore(Integer rugstoreId) {
        return drugstoreMapper.delDrugstore(rugstoreId);
    }

    /**
     * 药仓 出库
     *
     * @param drugstore
     * @return
     */
    @Override
    public int exWarehouse(Drugstore drugstore) {
        drugstoreMapper.exWarehouse(drugstore);
        return drugstoreMapper.delDrugstoreNum(drugstore.getRugstoreId());
    }

    @Override
    public List<Drugstore> queryDrugstore(Drugstore drugstore) {
        return drugstoreMapper.queryDrugstore(drugstore);
    }

    /**
     * 清理近3个月内会过期的药品
     *
     * @return
     */
    @Override
    public int cleanExpiredDrugs() {
        // 查询近3个月会过期的药品
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 3);
        List<Drugstore> drugstores = drugstoreMapper.queryMedicinalDate(calendar.getTime());

        for (Drugstore drugstore : drugstores) {
            Record record = new Record();
            record.setJiluname(drugstore.getDrugstoreName());
            record.setJilutime(new Date());
            record.setJilupihao(IdWorker.getNextId(1, 1, 1).toString());
            record.setJilunumber(drugstore.getDrugstorenum());
            recordMapper.addRecord(record); // 添加销毁记录
        }

        // 销毁药品
        return drugstoreMapper.cleanExpiredDrugs(drugstores);
    }

    @Override
    //查询库房药品信息
    public List<Drugstore> selStore(Drugstore drugstore) {
        return drugstoreMapper.selStore(drugstore);
    }

    @Override
    public List<Type> seltype(Type type) {
        return drugstoreMapper.seltype(type);
    }

    @Override
    public List<Area> selarea(Area area) {
        return drugstoreMapper.selarea(area);
    }

    @Override
    public List<Unit> selunit(Unit unit) {
        return drugstoreMapper.selunit(unit);
    }

    @Override
    public List<Skull> selskull(Skull skull) {
        return drugstoreMapper.selskull(skull);
    }

    @Override
    public List<Upplier> selupplier(Upplier upplier) {
        return drugstoreMapper.selupplier(upplier);
    }

    @Override
    public int updrugstore(Drugstore drugstore) {
        return drugstoreMapper.updrugstore(drugstore);
    }

    @Override
    public List<Warehuose> selwarehuose(Warehuose warehuose) {
        return drugstoreMapper.selwarehuose(warehuose);
    }


    @Override
    public List<Drugstore> selectlackdrug(Drugstore drugstore) {
        List<Drugstore> drugstores = drugstoreMapper.selectlackdrug(drugstore);

        for (int i=0; i<drugstores.size(); i++) {
            for (Caigou caigou : drugstoreMapper.selcaigou(new Caigou())) {
                if (drugstores.get(i).getDrugstoreName().equals(caigou.getCaigouname()) && caigou.getGonghuoshang().equals(drugstores.get(i).getWarehouseId().toString())) {
                    if (30 - drugstores.get(i).getDrugstorenum() <= caigou.getShuliang()) {
                        drugstores.remove(drugstores.get(i));
                    }
                }
            }
        }


        return drugstores;
    }

    @Override
    public int selcountless() {
        return drugstoreMapper.selcountless();
    }

    @Override
    public int addcaigou(Caigou caigou) {
        return drugstoreMapper.addcaigou(caigou);
    }

    @Override
    public int selcaigouname(Caigou caigou) {
        return drugstoreMapper.selcaigouname(caigou);
    }

    @Override
    public int upcaigounumber(Caigou caigou) {
        return drugstoreMapper.upcaigounumber(caigou);
    }

    @Override
    public List<Caigou> selcaigou(Caigou caigou) {
        return drugstoreMapper.selcaigou(caigou);
    }

    @Override
    public int delcaigou(Integer caigouid) {
        return drugstoreMapper.delcaigou(caigouid);
    }

    @Override
    public List<Drugstore> seldrugDate(Drugstore drugstore) {
        return drugstoreMapper.seldrugDate(drugstore);
    }

    @Override
    public int selcountlessnum() {
        return drugstoreMapper.selcountlessnum();
    }

    @Override
    public int delguoqidurg(Integer rugstoreId) {
        return drugstoreMapper.delguoqidurg(rugstoreId);
    }
}